#!/bin/sh
#
# Copyright (C) 2000-2021 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
#
#  This file is driven by the parameters that are defined in
#    the file External-mingw32
#

usage()
{
   echo "usage: $0 [-h] [-C] [<dependency 1>] [<dependency 2>] ..."
   echo "       -h      Displays this usage"
   echo "       -C      Clobbers (overwrites) the source code by "
   echo "               reextracting the archive and reapplying the"
   echo "               patches."
   echo "       -A      Rebuild everything (wipes out most of depkgs)"
   echo ""
   echo "<dependency N> Optional dependency, If none are given then all"
   echo "               of them will be built."
   echo ""
   echo "Valid dependencies are:"
   grep -v '^#' < External-mingw32 | cut -d'|' -f1 | cut -d'_' -f1 | tr A-Z a-z | sort -u | awk '{ print "        " $1 }'
}

CLOBBER_SOURCE=
CLOBBER_ALL=

while getopts "hHCA" opt; do
   case ${opt} in
   H|h|\?) usage;exit 1;;
   C)      CLOBBER_SOURCE=true;;
   A)      CLOBBER_ALL=true;;
   esac
done

[ ${OPTIND} -gt 1 ] && shift `expr ${OPTIND} - 1`

cwd=`pwd`
cd `dirname $0`
SCRIPT_DIR=`pwd`

cd ../../..
TOP_DIR=`pwd`
TOP_DIR=${DEPKGS:-${TOP_DIR}}

if [ -e ${TOP_DIR}/cross-tools/mingw32/bin/mingw32-gcc ]; then
   cd ${TOP_DIR}/cross-tools/mingw32/bin
   BIN_DIR=`pwd`
   BASE=mingw32
elif [ -e /mingw/bin/mingw32-gcc ]; then
   BIN_DIR=/mingw/bin
   BASE=mingw32

elif which i686-w64-mingw32-gcc > /dev/null; then
   BIN_DIR=
   BASE=i686-w64-mingw32

else
   echo "The GCC cross compiler is not installed."
   echo "You must run build-win32-cross-tools first"
   exit 1
fi

[ ! -e ${TOP_DIR}/depkgs-mingw32 ] && mkdir ${TOP_DIR}/depkgs-mingw32
cd ${TOP_DIR}/depkgs-mingw32
DEPPKG_DIR=`pwd`

if [ "${CLOBBER_ALL}" = "true" ]
then
   echo "Clobbering ${DEPPKG_DIR}"
   echo "rm -rf bin include info lib man qt-out/ share src tools"
   rm -rf bin include info lib man qt-out/ share src tools
fi

export PATH=${BIN_DIR}:${PATH}

[ ! -e bin ] && mkdir bin
[ ! -e src ] && mkdir src
[ ! -e include ] && mkdir include
[ ! -e lib ] && mkdir lib

OLD_IFS=${IFS};IFS="|";
while read package url dir mkd; do
#  echo "Got package ${package}"
   case ${package} in
   \#*) ;;
   *) eval "URL_${package}=${url};DIR_${package}=${dir};MKD_${package}=${mkd}";;
        esac
done < ${SCRIPT_DIR}/External-mingw32
IFS=${OLD_IFS};unset OLD_IFS


get_source()
{
   URL=$1
   SRC_DIR=$2
   MAKE_DIR=$3
   echo "Processing ${URL}"
   ARCHIVE=`basename ${URL}`
   
   case ${ARCHIVE} in
   *.tar.gz)       ARCHIVER="tar xzf";    [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.tar\.gz'`;;
   *.tar.bz2)      ARCHIVER="tar xjf";    [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.tar\.bz2'`;;
   *.zip)          ARCHIVER="unzip -q";   [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.zip'`;;
   *.exe)          ARCHIVER="";           [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.zip'`;;
   *.xz)       ARCHIVER="tar xf";    [ -z "${SRC_DIR}" ] && SRC_DIR=`expr "${ARCHIVE}" : '\(.*\)\.tar\.xz'`;;
   *)              echo Unsupported archive type - $ARCHIVE; exit 1;;
   esac
   
   cd ${DEPPKG_DIR}/src
   
   if [ ! -e "${ARCHIVE}" ]
   then 
      echo Downloading "${URL}"
      if wget --passive-ftp "${URL}"
      then
         :
      else
         echo Unable to download ${ARCHIVE}
         exit 1
      fi
   fi

   [ -z "${ARCHIVER}" ] && return 0

   if [ ! -e "${SRC_DIR}" -o "${CLOBBER_SOURCE}" = "true" ]
   then
      rm -rf ${SRC_DIR}
      echo Extracting ${ARCHIVE}
      if [ "${MAKE_DIR}" = "true" ]
      then
         mkdir ${SRC_DIR}
         cd ${SRC_DIR}
         ${ARCHIVER} ../${ARCHIVE} > ../${ARCHIVE}.log 2>&1
      else
         ${ARCHIVER} ${ARCHIVE} > ${ARCHIVE}.log 2>&1
         cd ${SRC_DIR}
      fi
      return 0
   fi

   cd ${SRC_DIR}
   return 1
}

parse_output()
{
   sed -ne '/\\$/N' -e 's/\\\n//' -e 's/\t\+/ /g' -e 's/ \+/ /g' \
       -e '/ error: /p' \
       -e "s%.*Entering directory[ ]\\+.${DEPPKG_DIR}/\\([^ ]\+\).%Entering \\1%p" \
       -e "s%.*Leaving directory[ ]\\+.${DEPPKG_DIR}/\\([^ ]\+.\).%Leaving \\1%p" \
       -e '/gcc \|g\+\+ \|ar /!d' \
       -e 's/ \(\.\.\/\)\+/ /g' \
       -e 's/.* \([^ ]\+\(\.c\|\.cpp\|\.cc\|\.cxx\)\)\( .*\|\)$/Compiling \1/p' \
       -e 's/.* \([^ ]\+\.s\)\( .*\|\)$/Assembling \1/p' \
       -e 's/.*ar [^ ]\+ \([^ ]\+\)\(\( [^ ]\+\.o\)\+\)/Updating \1 -\2/p' \
       -e 's/.* -o \([^ ]\+\)\( .*\|\)$/Linking \1/p'
}

do_patch()
{
   PATCH_FILE=${SCRIPT_DIR}/patches/$1; shift
   
   if patch -f -p0 "$@" >>patch.log < ${PATCH_FILE}
   then
      :
   else
      echo "Patch failed - Check `pwd`/patch.log" > /dev/tty
      exit 1
   fi
}

do_make()
{
   if make -f "$@" 2>&1
   then
      :
   else
      echo "Make failed - Check `pwd`/make.log" > /dev/tty
      exit 1
   fi | tee -a make.log | parse_output
}

process_lzo()
{
   if get_source "${URL_LZO}" "${DIR_LZO}" "${MKD_LZO}"
   then
       true
   fi
   echo Building lzo
   ./configure --host=$BASE --prefix=${DEPPKG_DIR}/
   echo Installing lzo
   do_make Makefile  PREFIX=${DEPPKG_DIR}/ all
   do_make Makefile  PREFIX=${DEPPKG_DIR}/ install
}

process_zlib()
{
   if get_source "${URL_ZLIB}" "${DIR_ZLIB}" "${MKD_ZLIB}"
   then
        true
   fi
   echo Building zlib
   > make.log
   do_make win32/Makefile.gcc PREFIX=${BASE}-  DESTDIR=${DEPPKG_DIR}/ all
   echo Installing zlib
   do_make win32/Makefile.gcc PREFIX=${BASE}-  DESTDIR=${DEPPKG_DIR}/ LIBRARY_PATH=lib BINARY_PATH=bin INCLUDE_PATH=include SHARED_MODE=1 install
}


process_pcre()
{
   if get_source "${URL_PCRE}" "${DIR_PCRE}" "${MKD_PCRE}"
   then
           echo Patching PCRE
           >patch.log
           do_patch pcre.patch
           echo Configuring PCRE
           ./configure CC_FOR_BUILD=gcc \
                       CXX_FOR_BUILD=g++ \
                       --host=$BASE \
                       --prefix=${DEPPKG_DIR} \
                       --enable-utf8 \
                       --enable-unicode-properties >make.log 2>&1
   fi
   echo Building PCRE
   do_make Makefile PREFIX=${DEPPKG_DIR} all
   echo Installing PCRE
   do_make Makefile PREFIX=${DEPPKG_DIR} install
}

process_db()
{
   if get_source "${URL_DB}" "${DIR_DB}" "${MKD_DB}"
   then
          echo No Patch
   fi
   cd build_unix
   ../dist/configure --host=$BASE --enable-mingw --prefix=${DEPPKG_DIR}
   > make.log
   echo Building DB
   do_make Makefile
   echo Installing DB
   do_make Makefile install_setup install_include install_lib
}

process_pthreads()
{
   if get_source "${URL_PTHREADS}" "${DIR_PTHREADS}" "${MKD_PTHREADS}"
   then
      sed -i '140 s/INLINE//' pthread_mutex_consistent.c
   fi
   echo Building pthreads
   > make.log
   do_make GNUmakefile CROSS=${BASE}- clean GCE
   echo Installing pthreads
   rm -rf ${DEPPKG_DIR}/include/pthreads
   mkdir ${DEPPKG_DIR}/include/pthreads
   cp -p *.h ${DEPPKG_DIR}/include/pthreads
   cp -p *.dll ${DEPPKG_DIR}/bin
   cp -p *.a ${DEPPKG_DIR}/lib
}

process_openssl()
{
   if get_source "${URL_OPENSSL}" "${DIR_OPENSSL}" "${MKD_OPENSSL}"
   then
        true
   fi
   echo Configuring openssl
   CROSS_COMPILE=${BASE}- ./Configure --prefix=${DEPPKG_DIR} \
                       shared zlib-dynamic \
                       threads \
                       --with-zlib-include=${DEPPKG_DIR}/include \
                       mingw > make.log 2>&1
   do_make Makefile all
   echo Installing openssl
   do_make Makefile install_sw
   mkdir -p ${DEPPKG_DIR}/ssl
   cp -f apps/openssl.cnf ${DEPPKG_DIR}/ssl
}

process_mysql()
{
   get_source "${URL_MYSQL}" "${DIR_MYSQL}" "${DIR_MYSQL}" "${MKD_MYSQL}"
   echo Converting mysql lib file
   if [ -e ${BIN_DIR}/reimp ]
   then
           reimp --dlltool ${BIN_DIR}/${BASE}-dlltool --as ${BIN_DIR}/${BASE}-as lib/opt/libmysql.lib
   else
           ${BIN_DIR}/../mingw32/bin/reimp --dlltool ${BIN_DIR}/${BASE}-dlltool --as ${BIN_DIR}/${BASE}-as lib/opt/libmysql.lib
   fi
   echo Installing mysql
   cp -p liblibmysql.a ../../lib/libmysql.a
   rm -rf ../../include/mysql
   mkdir ../../include/mysql
   cp -p include/* ../../include/mysql 2>&1 | grep -v 'omitting directory'
   cp -p lib/opt/libmysql.dll ../../bin
}

process_postgresql()
{
   if get_source "${URL_POSTGRESQL}" "${DIR_POSTGRESQL}" "${MKD_POSTGRESQL}"
   then
           echo Patching postgreSQL
           >patch.log
           do_patch postgresql.patch

           # We need a native version of zic to build the timezone tables.
           echo Configuring postgreSQL to build native zic
           ./configure > make.log 2>&1

           echo Building native zic
           cd src/timezone
           do_make Makefile >> make.log 2>&1
           cp zic ../..
           cd ../..
           do_make GNUmakefile distclean >> make.log 2>&1

           echo Configuring postgreSQL for MinGW32

           ./configure --host=$BASE \
                       --enable-shared \
                       --enable-thread-safety \
                       --prefix=${DEPPKG_DIR} \
                       --with-includes=${DEPPKG_DIR}/include:${DEPPKG_DIR}/include/pthreads \
                       --with-libraries=${DEPPKG_DIR}/lib >> make.log 2>&1
   fi
   echo Building postgreSQL
   ZIC=`pwd`/zic
   do_make GNUmakefile AR=$BASE-ar DLLTOOL=$BASE-dlltool DLLWRAP=$BASE-dllwrap WINDRES=$BASE-windres PTHREAD_LIBS=-lpthreadGCE ZIC=${ZIC}
   echo Installing postgreSQL
   do_make GNUmakefile AR=$BASE-ar DLLTOOL=$BASE-dlltool DLLWRAP=$BASE-dllwrap WINDRES=$BASE-windres PTHREAD_LIBS=-lpthreadGCE ZIC=${ZIC} install
}

process_sqlite()
{
   if get_source "${URL_SQLITE}" "${DIR_SQLITE}" "${MKD_SQLITE}"
   then
           echo Patching SQLite
           >patch.log
           do_patch sqlite.patch
   fi
   echo Building SQLite
   [ ! -e bld ] && mkdir bld
   cd bld
   > make.log
   do_make ../Makefile.mingw32 CROSSTOOLS=${BIN_DIR} TLIBS="-L${DEPPKG_DIR}/lib" TCL_FLAGS="-I${DEPPKG_DIR}/include"
   echo Installing SQLite
   cp -p sqlite3.exe ${DEPPKG_DIR}/bin
   cp -p libsqlite3.a ${DEPPKG_DIR}/lib
   cp -p sqlite3.h ${DEPPKG_DIR}/include
}

process_wx()
{
   if get_source "${URL_WX}" "${DIR_WX}" "${MKD_WX}"
   then
      echo Patching wxWidgets
      >patch.log
      cp build/msw/config.gcc build/msw/config.mingw32
      do_patch wxWidgets.patch
      find . -name makefile.gcc -exec sh -c "sed -f ${SCRIPT_DIR}/patches/wx.sed {} > \`echo {} | sed -e 's/\.gcc$/\.mingw32/'\`" \;
   fi
   echo Building wxWidgets
   cd build/msw
   > make.log
   do_make makefile.mingw32 SHARED=1 VENDOR=bacula DEBUG_INFO=1
   echo Installing wxWidgets
   cd ../..
   rm -rf ../../include/wx
   mkdir ../../include/wx
   cp -p include/wx/* ../../include/wx 2>&1 | grep -v 'omitting directory'
   mkdir ../../include/wx/generic
   cp -p include/wx/generic/* ../../include/wx/generic 2>&1 | grep -v 'omitting directory'
   mkdir ../../include/wx/msw
   cp -p include/wx/msw/* ../../include/wx/msw 2>&1 | grep -v 'omitting directory'
   mkdir ../../include/wx/msw/ole
   cp -p include/wx/msw/ole/* ../../include/wx/msw/ole 2>&1 | grep -v 'omitting directory'
   cp -p lib/gcc_dll/*.dll ../../bin
   rm -rf ../../lib/wx_dll
   mkdir ../../lib/wx_dll
   cp -p lib/gcc_dll/*.a ../../lib/wx_dll
   mkdir ../../lib/wx_dll/msw
   cp -p lib/gcc_dll/msw/* ../../lib/wx_dll/msw 2>&1 | grep -v 'omitting directory'
   mkdir ../../lib/wx_dll/msw/wx
   cp -p lib/gcc_dll/msw/wx/* ../../lib/wx_dll/msw/wx 2>&1 | grep -v 'omitting directory'
   mkdir ../../lib/wx_dll/msw/wx/msw
   cp -p lib/gcc_dll/msw/wx/msw/* ../../lib/wx_dll/msw/wx/msw 2>&1 | grep -v 'omitting directory'
}

process_scons()
{
   get_source "${URL_SCONS}" "${DIR_SCONS}" "${MKD_SCONS}"
   echo "Installing scons"
   if python setup.py install --prefix=${DEPPKG_DIR}/scons > make.log 2>&1
   then
           :
   else
           echo "Make failed - Check `pwd`/make.log"
           exit 1
   fi
}

process_nsis()
{
   get_source "${URL_NSIS_BIN}" "${DIR_NSIS_BIN}" "${MKD_NSIS_BIN}"
   cd ..
   rm -rf ../nsis
   mv nsis-2.17 ../nsis
   if get_source "${URL_NSIS_SRC}" "${DIR_NSIS_SRC}" "${MKD_NSIS_SRC}"
   then
           echo "Patching nsis"
           >patch.log
           do_patch nsis.patch
   fi
   echo "Building nsis"
   if ../../scons/bin/scons SKIPSTUBS=all SKIPPLUGINS=all SKIPUTILS=all SKIPMISC=all \
           PREFIX=${DEPPKG_DIR}/nsis PREFIX_BIN=${DEPPKG_DIR}/nsis/Bin \
           PREFIX_CONF=${DEPPKG_DIR}/nsis PREFIX_DATA=${DEPPKG_DIR}/nsis \
           PREFIX_DOC=${DEPPKG_DIR}/nsis/Docs 2>&1 | tee make.log | parse_output
   then
           :
   else
           echo "Scons failed - Check `pwd`/make.log"
           exit 1
   fi
   echo "Installing nsis"
   cp -p build/release/makensis/makensis ../../nsis
}

process_mtx()
{
   if get_source "${URL_MTX}" "${DIR_MTX}" "${MKD_MTX}"
   then
           echo Patching mtx
           # We can't run configure in a cross-compile environment so we
           # patch the files to the correct values
           cp -f config.h.in config.h
           cp -f Makefile.in Makefile
           rm -f configure
           >patch.log
           do_patch mtx.patch
   fi
   echo Building mtx
   do_make Makefile prefix=${DEPPKG_DIR} all
   echo Installing mtx
   do_make Makefile prefix=${DEPPKG_DIR} install
}

process_mt()
{
   if get_source "${URL_MT}" "${DIR_MT}" "${MKD_MT}"
   then
           echo "Patching mt"
           >patch.log
           do_patch mt.patch
   fi
   echo "Building mt"
   do_make Makefile PREFIX=${DEPPKG_DIR} all
   echo Installing mt
   do_make Makefile PREFIX=${DEPPKG_DIR} install
}

process_sed()
{
   if get_source "${URL_SED}" "${DIR_SED}" "${MKD_SED}"
   then
           echo Patching sed
           >patch.log
           do_patch sed.patch
           echo Configuring sed
           ./configure --host=$BASE \
                       --prefix=${DEPPKG_DIR} \
                       --disable-nls >make.log 2>&1
   fi
   echo Building sed
   do_make Makefile all
   echo Installing sed
   do_make Makefile install
}

process_stab2cv()
{
   if get_source "${URL_STAB2CV}" "${DIR_STAB2CV}" "${MKD_STAB2CV}"
   then
           echo Patching stab2cv
           >patch.log
           do_patch stab2cv.patch
           echo Configuring stab2cv
           ./configure --prefix=${DEPPKG_DIR}/tools \
                       >make.log 2>&1
   fi
   echo Building stab2cv
   do_make Makefile 
   echo Installing stab2cv
   do_make Makefile install
}

process_cmd_utils()
{
   if get_source "${URL_CMD_UTILS}" "${DIR_CMD_UTILS}" "${MKD_CMD_UTILS}"
   then
           # echo Patching cmd-utils
           # >patch.log
           # do_patch cmd-utils.patch
           sed -i "s:strrchr:NULL;//:" expr64/expr64.cpp
           echo Configuring cmd-utils
           ./configure --host=$BASE \
                       --prefix=${DEPPKG_DIR} \
                       >make.log 2>&1
   fi
   echo Building cmd-utils
   do_make Makefile 
   echo Installing cmd-utils
   do_make Makefile install
}

process_mkisofs()
{
   get_source "${URL_MKISOFS}" "${DIR_MKISOFS}" "${MKD_MKISOFS}"
   echo Installing mkisofs
   cp `basename ${URL_MKISOFS}` ${DEPPKG_DIR}/bin
}

process_dvd_rw_tools()
{
   if get_source "${URL_DVD_RW_TOOLS}" "${DIR_DVD_RW_TOOLS}" "${MKD_DVD_RW_TOOLS}"
   then
           echo Patching dvd+rw-tools
           >patch.log
           do_patch dvd+rw-tools.patch
   fi
   echo "Building dvd+rw-tools"
   m4 -DOS=XMINGW32 Makefile.m4 | make -f - dvd+rw-tools >make.log 2>&1
   echo "Installing dvd+rw-tools"
   m4 -DOS=XMINGW32 Makefile.m4 | make -f - prefix=${DEPPKG_DIR} manprefix=${DEPPKG_DIR} install >>make.log 2>&1
}

process_qt5()
{
    if get_source "${URL_Qt5}" "${DIR_Qt5}" "${MKD_Qt5}"
   then
       echo Patching Qt5
       do_patch qt5-qaction.patch
    fi
    if [ -f done ]; then
        return
    fi
    export OPENSSL_LIBS="-lssl -lcrypto"
    echo y | ./configure -xplatform win32-g++ -device-option CROSS_COMPILE=/usr/bin/i686-w64-mingw32- \
                -prefix ${DEPPKG_DIR} -opensource  -no-compile-examples \
                -no-openvg -no-sm -no-opengl -nomake examples -nomake tests \
                -skip qtactiveqt -skip qtcharts -skip qtdoc -skip qtlocation \
                -skip qtremoteobjects -skip qtserialbus -skip qtwebchannel \
                -skip qtwebview -skip qtandroidextras -skip qtconnectivity \
                -skip qtgamepad -skip qtmacextras -skip qtpurchasing -skip qtscript \
                -skip qttranslations -skip qtwebengine -skip qtwinextras \
                -skip qtdatavis3d -skip qtgraphicaleffects -skip qtmultimedia \
                -skip qtquickcontrols -skip qtscxml -skip qtspeech \
                -skip qtvirtualkeyboard -skip qtwebglplugin -skip qtx11extras \
                -skip qt3d -skip qtcanvas3d -skip qtdeclarative \
                -skip qtimageformats -skip qtquickcontrols2 \
                -skip qtsensors -skip qtwayland -skip qtwebsockets -silent -openssl-linked \
                -I $DEPPKG_DIR/include -L $DEPPKG_DIR/lib

    make
    make install

    cd qtbase
    cp -f ./plugins/platforms/qwindows.dll $DEPPKG_DIR/bin
    cp -r ./plugins/platforms $DEPPKG_DIR/bin
    cp -r ./plugins/platforms $DEPPKG_DIR/lib

    cp -rf src/corelib ${DEPPKG_DIR}/src/
    cp -rf src/gui ${DEPPKG_DIR}/src/
    cd $DEPPKG_DIR/src
}

process_qt4()
{
   if get_source "${URL_Qt4}" "${DIR_Qt4}" "${MKD_Qt4}"
   then
      echo Patching Qt4
      >patch.log
      patch -p1 < ${SCRIPT_DIR}/patches/qt4-intrinsics.patch
      patch -p1 < ${SCRIPT_DIR}/patches/qt4-widget-ui.patch
      patch -p1 < ${SCRIPT_DIR}/patches/qt4-compilation-see.patch
      patch -p1 < ${SCRIPT_DIR}/patches/qt4-compilation.patch
   fi
   echo "Configuring Qt4"
   ./configure -opensource -confirm-license -fast -xplatform win32-g++-8.3 \
   -device-option CROSS_COMPILE=i686-w64-mingw32- -device-option \
   PKG_CONFIG='i686-w64-mingw32-pkg-config' -force-pkg-config -release \
   -exceptions -shared -prefix ${DEPPKG_DIR}/qt-out -prefix-install -no-script \
   -no-iconv -no-webkit -no-glib -no-gstreamer -no-phonon -no-phonon-backend \
   -accessibility -no-reduce-exports -no-rpath -make libs -nomake demos \
   -nomake docs -nomake examples -system-zlib -no-mitshm -no-libjpeg \
   -no-libmng -no-libtiff -no-sql-db2 -no-sql-ibase -no-sql-mysql -no-sql-oci \
   -no-sql-odbc -no-sql-psql -no-sql-sqlite -no-sql-sqlite2 \
   -no-sql-sqlite_symbian -no-sql-symsql -no-sql-tds -no-nis -no-cups -no-dbus \
   -no-openvg -no-nas-sound -no-audio-backend -no-sm -no-opengl \
   -no-javascript-jit -no-qt3support -nomake tools \
   -no-xmlpatterns -no-multimedia -nomake tools -silent

   make

   echo "Installing Qt4"
   rm -rf ${DEPPKG_DIR}/include/qt ${DEPPKG_DIR}/lib/qt
   mkdir -p ${DEPPKG_DIR}/include/qt ${DEPPKG_DIR}/lib/qt
   cp -rf include/* ${DEPPKG_DIR}/include/qt
   cp -rf lib/* ${DEPPKG_DIR}/lib/qt
   cp -rf src/corelib ${DEPPKG_DIR}/src/
   cp -rf src/gui ${DEPPKG_DIR}/src/
   cp -rf lib/QtCore4.dll lib/QtGui4.dll ${DEPPKG_DIR}/bin/
   cp -rf lib/QtCore4.dll lib/QtGui4.dll ${DEPPKG_DIR}/lib/qt/
}

process_mingw()
{
   if test -f /usr/lib/gcc/$BASE/*posix/libstdc++-6.dll; then
      echo "Installing MinGW libs"
      cp /usr/lib/gcc/$BASE/*posix/libstdc++-6.dll ${DEPPKG_DIR}/bin
      cp /usr/lib/gcc/$BASE/*posix/libgcc*dll ${DEPPKG_DIR}/bin
   elif test -f /usr/lib/gcc/$BASE/*/libstdc++-6.dll; then
      echo "Installing MinGW libs"
      cp /usr/lib/gcc/$BASE/*/libstdc++-6.dll ${DEPPKG_DIR}/bin
      cp /usr/lib/gcc/$BASE/*/libgcc*dll ${DEPPKG_DIR}/bin
   elif test -f /usr/$BASE/lib/libstdc++-6.dll; then
      echo "Installing MinGW libs"
      cp /usr/$BASE/lib/libstdc++-6.dll ${DEPPKG_DIR}/bin
      cp /usr/$BASE/lib/libgcc*dll ${DEPPKG_DIR}/bin
   else
      echo "ERROR: Unable to find $BASE on this system"
   fi

   if test -f /usr/$BASE/lib/libwinpthread-1.dll; then
      cp /usr/$BASE/lib/libwinpthread-1.dll ${DEPPKG_DIR}/bin
   fi
}

process_zstd()
{
   echo Installing ZSTD
   get_source "$URL_ZSTD" "$DIR_ZSTD" "$MKD_ZSTD"
   CC=i686-w64-mingw32-gcc make TARGET_SYSTEM=Windows
   cp  lib/libzstd.a $DEPPKG_DIR/lib
   cp  lib/*.h $DEPPKG_DIR/include
}

if [ "$#" -eq 0 ]
then
   process_mingw
   process_lzo
   process_zlib
   #process_pthreads
   process_openssl
   process_stab2cv
   process_sed
   process_cmd_utils
   process_zstd
#   process_pcre
#   process_mysql
#   process_sqlite
#   process_postgresql
#   process_wx
#   process_scons
#   process_nsis
#   process_mtx
#   process_mt
#   process_mkisofs
#   process_dvd_rw_tools
   process_qt5
else
   for dependency in "$@"
   do
      eval "process_${dependency}"
   done
fi
#vss
#Need to download from Microsoft
